package com.qianyu.project.web.controller.hyh;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.entity.LeaseContract;
import com.qianyu.project.entity.LeasePayment;
import com.qianyu.project.entity.SubCompany;
import com.qianyu.project.entity.SystemUser;
import com.qianyu.project.mapper.SystemUserMapper;
import com.qianyu.project.service.ILeaseContractService;
import com.qianyu.project.service.ILeasePaymentService;
import com.qianyu.project.service.ISubCompanyService;
import com.qianyu.project.service.ISystemUserService;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Action;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author hyh
 * @date 2021/2/1010:24
 */
@RestController
@RequestMapping("leasePayment")
public class LeasePaymentController {
    @Autowired
    ILeasePaymentService iLeasePaymentService;
    @Autowired
    ILeaseContractService iLeaseContractService;
    @Autowired
    ISubCompanyService iSubCompanyService;
    @Autowired
    SystemUserMapper systemUserMapper;



    @GetMapping("")
    public List<LeaseContract>  getLeasecontract(){
        return  iLeaseContractService.list();
    }

    @GetMapping("/getCompanyName")
    public  List<SubCompany> getCompanyName(){
        return  iSubCompanyService.list();
    }

    @GetMapping("/getMaxid")
    public Result getMaxid(){
        return ResultTool.success(Long.parseLong(iLeasePaymentService.getMaxid()) + 1);

    }
    @PostMapping("/saveLeasePayment")
    public void saveLeasePayment(@RequestBody Map<String,Object> map){
        System.out.println(map);
        LeasePayment leasePayment=new LeasePayment();
        leasePayment.setId(map.get("id").toString());
        leasePayment.setPId(map.get("pid").toString());
        leasePayment.setApplyDate(map.get("applyDate").toString());
        leasePayment.setContractName("contractName");
        leasePayment.setCtrId(map.get("ctrId").toString());
        String str=  map.get("contractAmount").toString();
        BigDecimal ret = new BigDecimal(str);

        leasePayment.setContractAmount(ret);
        SystemUser systemUser = systemUserMapper.selectOne(new QueryWrapper<SystemUser>().eq("user_realname", map.get("handlerId")));
        long id = systemUser.getId();
        leasePayment.setHandlerId((int)id);
        iLeasePaymentService.save(leasePayment);

        System.out.println(leasePayment);

    }
    //查询全部付款信息
    @GetMapping("/listPaymentlist")
    public List<LeasePayment> listPaymentlist(@RequestParam Map<String,String> map){

        System.out.println(map);
        QueryWrapper qw=new QueryWrapper();
        String id=map.get("id");
        if(StringUtils.isNotBlank(id)){
            qw.eq("id",id);
        }
        String contract_amount=map.get("contract_amount");
        String contract_amount2=map.get("contract_amount2");

        if(StringUtils.isNotBlank(contract_amount)&&StringUtils.isNotBlank(contract_amount2)){
            qw.between("contract_amount",contract_amount,contract_amount2);
        }

        String pid=map.get("pid");
        if(StringUtils.isNotBlank(pid)){
            qw.eq("p_id",pid);

        }

        String order=map.get("order");
        if(StringUtils.isNotBlank(order)){
            String[] orderargs=order.split("\\.");
            if(orderargs[1].equals("desc")){
                qw.orderByDesc(orderargs[0]);
            }else{
                qw.orderByAsc(orderargs[0]);
            }
        }
        String status=map.get("approve_status");
        if(StringUtils.isNotBlank(status)){
            qw.eq("approve_status",status);
        }

        return iLeasePaymentService.list(qw);
    }
}
